// Replicate wc -l (so far, for files up to 100K at least, speed matches)

# include <stdio.h>

int lines_in(char *file) { 
    // Count the number of lines in the file
    
    // int character; 
    int bytes = 0; 
    FILE *fp; 
    
    fp = fopen(file, "r"); 
    
    for ( bytes = 0; fgetc(fp) != EOF ; ++bytes ); 

    /* Old
    while ( (character = fgetc(fp)) != EOF ) {
        // if ASCII decimal 10, "\n", or 13, "\r", shows up, count it
        if ( (character == 10) || (character == 13) ) { 
            count += 1; 
        }
    }
    */
    
    int fclose(FILE *fp); 
    
    return bytes; 
}

// Python comparison
// "int argc" => len(argv)
// "*argv[]" => sys.argv[]
int main( int argc, char *argv[] ) {
    // Dummy variables
    int each; 
    
    // If no file name specified, print usage
    if (argc < 2) {
        printf("Try: \"%s filename\"\n", argv[0]); 
    }
    
    // Otherwise, for each line in each file, calculate lines
    for (each = 1; each < argc; each += 1) {
        printf(
            "%s: %d\n", argv[each], lines_in(argv[each])
        );
    }
}
